/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xunome.game.multiplayerg;
import xunome.xUnoME;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.StringItem;
import java.util.Vector;
import xunome.game.Card;
import xunome.game.SelectionMove;
/**
*
* @author Sem iNick
*/
public final class MultiPlayerManager implements CommandListener {
private MultiPlayer game;
private Form chatForm;
private xUnoME mid;
private boolean isServer;
private TextField msg;
private Vector msgs;
private String name;
private ConnectionServer server;
private DataListener comunication;
private final Command sendMsg = new Command("Send", Command.ITEM, 1);
private final Command start = new Command("Play", Command.OK, 0);
private final Command back = new Command("Back", Command.BACK, 2);
private final int MAX_MSG = 10;
public MultiPlayerManager(xUnoME mid, String name) {
this.isServer = false;
this.name = name;
this.mid = mid;
comunication = new DataListener(this);
msgs = new Vector();
msg = new TextField("Message:", "", 40, TextField.ANY);
chatForm = new Form("xUNO ME");
chatForm.append(msg);
chatForm.setCommandListener(this);
chatForm.addCommand(sendMsg);
chatForm.addCommand(back);
mid.getLCD().setCurrent(chatForm);
}
public MultiPlayerManager(ConnectionServer con, xUnoME mid, String name) {
this(mid, name);
server = con;
isServer = true;
chatForm.addCommand(start);
}
public void commandAction(Command c, Displayable d) {
if (c.equals(sendMsg)) {
String getMsg = msg.getString();
if (getMsg.equals("")) {
return;
}
if (isServer) {
addMessage(getMsg, name);
}
comunication.sendData(DataListener.MESSAGE + name + DataListener.MSG_SEPARATOR
+ getMsg + DataListener.DATA_DELIMITER);
msg.setString("");
} else if(c.equals(start)) {
int nPlayers = comunication.getNumberListener();
if (nPlayers == 0) {
return;
}
game = new MultiPlayer(MultiPlayer.createDeck(), 0,
nPlayers + 1, this, mid);
game.getTable().shuffleDeck();
Vector deck = game.getTable().getDeck();
StringBuffer dataDeck = new StringBuffer();
for (int ac = 0; ac < deck.size(); ac++) {
Card getCard = (Card) deck.elementAt(ac);
dataDeck.append(DataListener.CARD_DELIMITER + getCard.toString());
}
comunication.sendInitialGame(dataDeck.toString(),
comunication.getNumberListener() + 1,
game.getAtualPlayerPos());
game.distributeCards();
game.putFirstCard();
game.getCanvas().refresh();
new SelectionMove(game).start();
mid.getLCD().setCurrent(game.getCanvas());
} else {
if (!isServer) {
comunication.sendData(DataListener.MESSAGE + "Server"
+ DataListener.MSG_SEPARATOR + getName()
+ " has desconnected." + DataListener.DATA_DELIMITER);
}
comunication.closeAllConnections();
destroy();
}
}
public synchronized void addMessage(String message, String sender) {
String[] newData = {sender, message};
if (msgs.size() == MAX_MSG) {
msgs.removeElementAt(0);
msgs.addElement(newData);
for (int i = 1, amsg = MAX_MSG - 1; i < chatForm.size(); i++, amsg--) {
String[] getMsg = (String[]) msgs.elementAt(amsg);
StringItem getField = (StringItem) chatForm.get(i);
getField.setLabel(getMsg[0] + " says:");
getField.setText(getMsg[1]);
}
} else {
chatForm.insert(1, new StringItem(sender + " says:", message));
msgs.addElement(newData);
}
}
public boolean isServer() {
return isServer;
}
public DataListener getDataListener() {
return comunication;
}
public void showChatForm() {
game = null;
mid.getLCD().setCurrent(chatForm);
}
public void createGame(Vector deck, int pos, int nplayer, int atual) {
game = new MultiPlayer(deck, pos, nplayer, this, mid);
game.setAtualPlayer(atual);
game.distributeCards();
game.putFirstCard();
game.getCanvas().refresh();
new SelectionMove(game).start();
mid.getLCD().setCurrent(game.getCanvas());
}
public MultiPlayer getGame() {
return game;
}
public String getName() {
return name;
}
public ConnectionServer getServer() {
return server;
}
public void destroy() {
if (isServer) {
server.stopListening();
}
mid.setMain();
}
}